﻿/// ---------------------------------------------
///
/// ---------------------------------------------

///---------------------------

//双击事件
function CustomDbClick() {
    //parent.closeWin(); //去掉双击功能
    //显示日历详细
    var recordID = myDataTable.getSelectedRows()[0];
    record = myDataTable.getRecord(recordID);
    if (record == null) {
        alert("请选择一条记录");
        return;
    }
    var id = record.getData(const_entityID);
    var pagetitle = "工作日历—" + record.getData("Name");
    var startyear = record.getData("BeginYear");
    var endyear = record.getData("EndYear");
    parent._employeeTitle = pagetitle;
    parent.addTabs(pagetitle, encodeURI("../EHR/calendar/calendar.aspx?r=" + Math.random() + "&id=" + id + "&startyear=" + startyear + "&endyear=" + endyear));
    parent.NeedRefreshPage = this;
}

//日历视图
var fnCalendarView = function () {
    var recordID = myDataTable.getSelectedRows()[0];
    record = myDataTable.getRecord(recordID);
    if (record == null) {
        alert("请选择一条记录");
        return;
    }
    var id = record.getData(const_entityID);
    var pagetitle = "工作日历—" + record.getData("Name");
    var startyear = record.getData("BeginYear");
    var endyear = record.getData("EndYear");
    parent._employeeTitle = pagetitle;
    parent.addTabs(pagetitle, encodeURI("../EHR/calendar/calendar.aspx?r=" + Math.random() + "&id=" + id+"&startyear="+startyear+"&endyear="+endyear));
    parent.NeedRefreshPage = this;
}

//添加信息
var fnCustomAdd = function () {
    const_EditEntity = "wkCalenderSolution";
    var pagetitle = "新增日历";
    parent._employeeTitle = pagetitle;
    parent.addTabs(pagetitle, encodeURI("../Extend/Edit.aspx?r=" + Math.random() + "&entity=" + const_EditEntity+"&fun=add"));
    parent.NeedRefreshPage = this;
}

//修改信息
var fnCustomEdit = function () {
    var recordID = myDataTable.getSelectedRows()[0];
    record = myDataTable.getRecord(recordID);
    if (record == null) {
        alert("请选择一条记录");
        return;
    }
    const_EditEntity = "wkCalenderSolution";
    var pagetitle = "修改日历——" + record._oData["Name"];
    var isdefault = record._oData["isDefault"];
    parent._employeeTitle = pagetitle;
    parent.addTabs(pagetitle, encodeURI("../Extend/Edit.aspx?r=" + Math.random() + "&entity=" + const_EditEntity + "&id=" + record.getData(const_entityID) + "&fun=edit"));
    parent.NeedRefreshPage = this;
}

var CalenderSolutionID = "";
//适用部门
var fnSelectDept = function () {
    var recordID = myDataTable.getSelectedRows()[0];
    record = myDataTable.getRecord(recordID);
    if (record == null) {
        alert("请选择一条记录");
        return;
    }
    CalenderSolutionID = record.getData(const_entityID);
    document.getElementById("dialog1_h").innerHTML = "适用部门";
    document.getElementById("EditUIWindow").src = "../EditWidows/YUITree.aspx?r=" + Math.random() + "&entity=DatDepartment&masterPara=DatDepartment|DeptName|DeptID|ParentID|DeptPath|True";
    SetDWH(550, 480, YAHOO.example.container.dialog1);
    YAHOO.example.container.dialog1.show();
    //setTimeout(getCheckedNodes, 200);
    getCheckedNodes();
}

//调用YUITree.aspx的ChooseInfo方法，获取选中部门、岗位
var getChooseInfo = function () {
    var editwindow = document.getElementById("EditUIWindow");
    if (editwindow) {
        editwindow.contentWindow.ChooseInfo();
    }
}

//返回选中部门
var _fn = function (ids, names) {
    if (ids == "" || ids == "undefined") return;
    //alert("ids:" + ids + "  names:" + names);
    //保存到数据库
    YAHOO.util.Connect.asyncRequest('POST', "../EHR/workTimeAjax.aspx?flag=savewkDeptCalenderSolution&ids=" + ids + "&CalenderSolutionID=" + CalenderSolutionID, {
        success: function (res) {
            try {
                var str = res.responseText;
                //debugger;
                if (str == "1") {
                    alert("保存成功！");
                }
                else {
                    if (str.substr(0, 1) == "e") {
                        alert(str.substr(1, str.length - 1));
                    }
                    else {
                        alert("保存失败！");
                    }
                }
            }
            catch (e) { }
            finally { }
        },
        failure: function (error) {
            alert("数据错误：" + error.statusText);
        }
    }, null);
    YAHOO.example.container.dialog1.hide();
}

//得到已经选择的部门
var getCheckedNodes = function () {
    //debugger;
    var editwindow = document.getElementById("EditUIWindow");
    var request = "../../Extend/AjaxData/DataProcessin.aspx?action=Get&entity=wkDeptCalenderSolution&condition=CalenderSolutionID='" + CalenderSolutionID + "'";
    YAHOO.util.Connect.asyncRequest('POST', request, {
        success: function (res) {
            var inventory;
            try {
                //debugger;
                var str = res.responseText;
                var ids = [];
                var items = eval("[" + str + "]");
                for (var i = 0; i < items[0].rows.length; i++) {
                    ids.push(items[0].rows[i].DeptID);
                }
                ids = ids.join(",");
                if (editwindow) {
                    editwindow.contentWindow.checkSelectedNode(ids);
                }
            }
            catch (e) {
            }
            finally {
            }
        },
        failure: function (error) {
            alert("读取节点数据错误：" + error.statusText);
        }
    }, null);
}

//初始化
if (typeof ($) != "undefined") {
    $(document).ready(function () {
        if (location.href.indexOf("YUIDataTableList.aspx") == -1) {
            var fun = getPar1("fun");
            if (fun == "add") {//新增
                //parent.setButtonHiden();
                //setTimeout(AddCreatDataButton,500);
                //            $("div input").attr("disabled", true).css("background", "whitesmoke");
                //            $("div select ").attr("disabled", true).css("background", "whitesmoke");
                setTimeout(HideButtonAndData, 200);
                //setTimeout(AddOneRecordToDetail, 200); //新增的时候
            }
            if (fun == "edit")//修改
            {
                setTimeout(AddHolidayValueChanged, 200); //延迟加载

            }
        }
        else {
            var btn = $("#yui-gen7-button");
            btn.click(function () {
                getChooseInfo();
            });
        }
    });
}
//给假期控件添加值改变事件
function AddHolidayValueChanged() {
    $("input[name=Holiday]").each(function () {
        $(this).blur(function () {
            if ($(this).val() != "") {
                var startdate = $("#BeginYear").val() + "-01-01";
                var enddate = $("#EndYear").val() + "-12-31";
                if ($(this).val() < startdate || $(this).val() > enddate) {
                    alert("日期介于" + startdate + "和" + enddate + "之间！");
                    $(this).val("");
                }
            }
        });
    });
}

//默认增加一条数据
function AddOneRecordToDetail() {
    //模仿单击新增按钮，新增一行
    _DataTable.AddRow();
    //获取开始年份，插入一条记录为开始年份的1月1日
    var startYear = $("#BeginYear").val();
    $("#Holiday").val(startYear + "-01-01");
    $("#DayType").val("1");
    //然后隐藏数据
    $("#wkCalendar").hide();
    //alert($("#wkCalendar button:last").text());
}
//给删除按钮增加mousedown事件，保证有一条记录存在，防止无明细数据时保存报错
function DeleteButtonMouseDown() {
    $("#wkCalendar button:last").mousedown(function () {
//        var rows = $("table[id$='wkCalendar']>tbody").children("tr").length;
//        if (rows == 1) {
//            alert("至少保留一条记录！");
//            return;
//        }
    });
}

//隐藏新增删除按钮
function HideButtonAndData() {
    $("#wkCalendar thead:first-child").hide();
    $("#wkCalendar").hide();
}

////添加一个生成数据按钮
//function AddCreatDataButton() {
//    $("#wkCalendar td:first").append("<input type='button' id='_btnCreateData' value='生成数据' onclick='CreateCalendarData();'>");
//    $("#wkCalendar button").hide();
//}

////生成日历数据
//function CreateCalendarData() {
//    var url = "../EHR/calendar/calendar.ashx?action=create";

//    $("#wkCalendar button").show();
//}

//删除第一步
var fnCustomDelete = function () {
    var recordID = myDataTable.getSelectedRows()[0];
    record = myDataTable.getRecord(recordID);
    if (record == null) {
        alert("请选择一条记录");
        return;
    }
    var id = record.getData(const_entityID);
    //第一步先删除相关的日历数据
    YAHOO.util.Connect.asyncRequest('POST', "../EHR/workTimeAjax.aspx?flag=deletewkCalenderSolution&id=" + id, {
        success: function (res) {
            try {
                var str = res.responseText;
                if (str == "0") {
                    //第二步
                    var list = mDataTable.getRecordSet()._records;
                    dl = [];
                    for (var i = 0; i < list.length; i++) {
                        if (list[i]._oData["iSun_Selected"] == true) {
                            dl.push(list[i]._sId); //获取行ID；
                        }
                    }
                    SaveUrl = "../Extend/AjaxData/DataSave.aspx?r=" + Math.random() + "&action=delete&entity=" + const_EditEntity + "&id=" + id;
                    YAHOO.example.container.dialog3.show();
                }
                else {
                    alert("日历删除失败！");
                    return;
                }
            }
            catch (e) {
                alert("删除失败：" + e.Message);
                return;
            }
            finally {
            }
        },
        failure: function (error) {
            alert("数据错误：" + error.statusText);
            return;
        }
    }, null);
}
